﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace AnaViewer
{
    public class ToolButton
    {
        public bool IsSeparator = false;
        /// <summary>
        /// 是否是右对齐。
        /// </summary>
        public bool RightToLeft = false;
        public Rectangle Rect = Rectangle.Empty;
        public string Text;
        public Image Image_Normal;
        public Image Image_Hover;
        public Image Image_Active;

        public Color Color_Active = Color.FromArgb(204, 51, 51);//红色
        public Color Color_Normal = Color.FromArgb(0,102,0);//绿色
        public Color Color_Hover = Color.FromArgb(204,51,51);//红色

        public ToolButton( string text, Image imgNormal, Image imgHover, Image imgActive)
        {
            this.Text = text;
            this.Image_Normal = imgNormal;
            this.Image_Hover = imgHover;
            this.Image_Active = imgActive;
        }
    }



    /// <summary>
    /// 提供呈现“工具栏”过程的封装。
    /// </summary>
    public class ToolBarView : ViewBase
    {
        List<ToolButton> buttons;
        /// <summary>
        /// 按钮项集合。
        /// </summary>
        public IList<ToolButton> Buttons
        {
            get { return buttons.AsReadOnly(); }
        }
        public void AddButton(ToolButton btn)
        {
            buttons.Add(btn);
            this.Invalidate();
        }

        /// <summary>
        /// 工具栏上的LOGO按钮。
        /// </summary>
        ToolButton headButton;
        public ToolButton HeadButton
        {
            get { return headButton; }
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logo">显示在工具栏头部的 Logo 图片。</param>
        public ToolBarView()
        {
            buttons = new List<ToolButton>();
            headButton = new ToolButton("",null,null,null);


            base.backColor = Color.FromArgb(150, Color.White);
        }

        public Image LogoImage
        {
            get { return headButton.Image_Normal; }
            set
            {
                if (headButton.Image_Normal == value)
                    return;

                headButton.Image_Normal = value;
                //使用此函数 重新计算工具栏各个按钮的位置。
                OnResize(null);
            }
        }

        Color borderColor = Color.FromArgb(255, Color.White);
        /// <summary>
        /// 描边的颜色。
        /// </summary>
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; OnStyleChanged(new EventArgs()); this.Invalidate(); }
        }
        Color hoverBackColor = Color.White;
        public Color HoverBackColor
        {
            get { return hoverBackColor; }
            set { hoverBackColor = value; this.Invalidate(); }
        }
        Color activeBackColor = Color.FromArgb(100, Color.White);
        public Color ActiveBackColor
        {
            get { return activeBackColor; }
            set { activeBackColor = value; this.Invalidate(); }
        }
        
        /// <summary>
        /// 查找某个位置的项。
        /// </summary>
        /// <param name="point"></param>
        /// <returns></returns>
        public ToolButton GetItemAt(Point point)
        {
            ToolButton btn = null;
            //ToolBar 内部相对位置
            Point p = new Point(point.X - bounds.X, point.Y - bounds.Y);
            foreach (ToolButton item in buttons)
            {
                if (item.Rect.Contains(p))
                {
                    btn = item;
                    break;
                }
            }
            return btn;
        }

        public int GetItemIndex(ToolButton item)
        {
            return buttons.IndexOf(item);
        }

        /// <summary>
        /// 是 鼠标停留在上面还是 鼠标点击（ isActive = true 为点击 ）
        /// </summary>
        bool isActive = false;
        public bool IsActive
        {
            get { return isActive; }
            set
            {
                if (isActive == value)
                    return;

                isActive = value;
                OnIsActiveChanged(new EventArgs());
                this.Invalidate();
            }
        }
        public event EventHandler IsActiveChanged;
        protected virtual void OnIsActiveChanged(EventArgs e)
        {
            if (IsActiveChanged != null)
                IsActiveChanged(this, e);
        }




        int selectedIndex = -1;
        /// <summary>
        /// 活动的项（当前的操作都针对该项）
        /// </summary>
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                if (selectedIndex == value)
                    return;

                selectedIndex = value;
                OnSelectedIndexChanged(new EventArgs());
                
                this.Invalidate();
            }
        }
        /// <summary>
        /// 活动的项（当前的操作都针对该项）
        /// </summary>
        public ToolButton SelectedItem
        {
            get
            {
                if (selectedIndex < 0 || selectedIndex >= buttons.Count)
                    return null;
                else
                    return buttons[selectedIndex];
            }
            set
            {
                this.SelectedIndex = buttons.IndexOf(value);
            }
        }
        public event EventHandler SelectedIndexChanged;
        protected virtual void OnSelectedIndexChanged(EventArgs e)
        {
            if (SelectedIndexChanged != null)
                SelectedIndexChanged(this, e);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            //重新计算各按钮的位置。按钮位置为内部相对ToolBar的位置
            Rectangle rect = new Rectangle(0,0,this.bounds.Width,this.bounds.Height);
            //Logo 图片的宽度
            int logoWidth = (headButton.Image_Normal == null) ? 0 : headButton.Image_Normal.Width;
            headButton.Rect = new Rectangle(rect.X, rect.Y, logoWidth, rect.Height);

            int x = logoWidth + 2; //rect.Height;//圆角
            int right = rect.Width - 2;
            foreach (ToolButton item in buttons)
            {
                int width = 0;

                if (item.IsSeparator)
                {//分隔线
                    width = 16;
                    Rectangle btnRect;
                    if (!item.RightToLeft)
                    {//左对齐
                        btnRect = new Rectangle(x + rect.X, 0 + rect.Y + 2, width, rect.Height - 4);
                        x += width;
                    }
                    else
                    {
                        btnRect = new Rectangle(right - width + rect.X, 0 + rect.Y + 2, width, rect.Height - 4);
                        right -= width;
                    }

                    //记录区域
                    item.Rect = btnRect;
                }
                else
                {//普通按钮，图标+文本
                    width = 16;//图标的大小
                    width += (int)TextRenderer.MeasureText(item.Text, this.Font).Width + 5;
                    Rectangle btnRect;
                    if (!item.RightToLeft)
                    {//左对齐
                        btnRect = new Rectangle(x + rect.X, 0 + rect.Y + 2, width, rect.Height - 4);
                        x += width;
                    }
                    else
                    {
                        btnRect = new Rectangle(right - width + rect.X, 0 + rect.Y + 2, width, rect.Height - 4);
                        right -= width;
                    }
                    //记录区域
                    item.Rect = btnRect;
                }
            }

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.InterpolationMode = InterpolationMode.Default;
            g.SmoothingMode = SmoothingMode.Default;
            Rectangle rect = e.ClipRectangle;

            using (Brush brush = new SolidBrush(backColor))
            //using (GraphicsPath path = Common.GetPath(rect,6))
            {
                //g.FillPath(brush, path);
                g.FillRectangle(brush, rect);
            }
            
            // 绘制 LOGO 按钮。
            if (headButton.Image_Normal != null)
            {
                using (Brush brush = new SolidBrush(borderColor))
                    g.FillRectangle(brush, headButton.Rect);
                g.DrawImageUnscaledAndClipped(headButton.Image_Normal, headButton.Rect);
            }

            foreach (ToolButton item in buttons)
            {
                Rectangle btnRect = item.Rect;

                if (item.IsSeparator)
                {//分隔线
                    using (Pen pen = new Pen(borderColor, 2))
                        g.DrawLine(pen, new Point( btnRect.X + btnRect.Width / 2, btnRect.Y+1), new Point(btnRect.X + btnRect.Width / 2, btnRect.Bottom-1));
                }
                else
                {//普通按钮，图标+文本
                    Color textColor = Color.Black;
                    Image image = null;
                    if (item == SelectedItem)
                    {//选中项，绘制高亮条
                        Color btnBackColor = Color.Transparent;
                        if (isActive)
                        {
                            image = item.Image_Active;
                            textColor = item.Color_Active;
                            btnBackColor = activeBackColor;
                        }
                        else
                        {
                            image = item.Image_Hover;
                            textColor = item.Color_Hover;
                            btnBackColor = hoverBackColor;
                        }

                        using (Brush brush = new SolidBrush(btnBackColor))
                            g.FillRectangle(brush, btnRect);
                    }
                    else
                    {
                        image = item.Image_Normal;
                        textColor = item.Color_Normal;
                    }

                    if (image != null)//绘制图标
                        g.DrawImageUnscaledAndClipped(image, new Rectangle(btnRect.X + (16 - image.Width) / 2, btnRect.Y + (btnRect.Height - image.Height) / 2, image.Width, image.Height));

                    using (StringFormat format = new StringFormat(StringFormatFlags.LineLimit))
                    {
                        format.Alignment = StringAlignment.Near;
                        format.LineAlignment = StringAlignment.Center;
                        g.DrawString(item.Text, this.Font, new SolidBrush(textColor), new Rectangle(btnRect.X + 16, btnRect.Y, btnRect.Width - 16, btnRect.Height), format);
                    }

                }

#if DEBUG_SHOWBORDER
                g.DrawRectangle(Pens.Lime, btnRect);
#endif

            }

            using (Pen pen = new Pen(borderColor, 1))
            { g.DrawRectangle(pen, new Rectangle(rect.X, rect.Y, rect.Width - 1, rect.Height - 1)); }

            base.OnPaint(e);
        }
    }
}
